package com.wechatapp.mengyao.poetry.controller;

import com.wechatapp.mengyao.poetry.core.JwtUtils;
import com.wechatapp.mengyao.poetry.core.RestResponse;
import com.wechatapp.mengyao.poetry.dto.*;
import com.wechatapp.mengyao.poetry.entity.UserInfo;
import com.wechatapp.mengyao.poetry.service.UserInfoService;
import com.wechatapp.mengyao.poetry.vo.LoginResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author yanghuaxu
 * @date 2025/5/20 19:37
 */
@RestController
@RequestMapping("/api/v1/user")
@Validated
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/login")
    public RestResponse<LoginResponse> login(@RequestBody LoginRequestDTO loginRequestDTO) {

        return RestResponse.ok(userInfoService.login(loginRequestDTO));
    }

    @PostMapping("/token")
    public RestResponse<String> generateToken(@RequestBody WeChatInfo weChatInfo) {

        return RestResponse.ok(JwtUtils.generateToken(weChatInfo.getOpenId(), weChatInfo.getUnionId()));
    }

    @PutMapping("")
    public RestResponse<UserInfo> updateUser(@RequestBody UserInfoDTO userInfoDTO) {

        return RestResponse.ok(userInfoService.updateUser(userInfoDTO));
    }

    @PutMapping("/avatar")
    public RestResponse<UserInfo> updateUserAvatar(@RequestBody UserAvatarDTO avatarDTO) {

        return RestResponse.ok(userInfoService.updateUserAvatar(avatarDTO));
    }

    @GetMapping("/{id}")
    public RestResponse<UserInfo> getUserById(@PathVariable String id) {

        return RestResponse.ok(userInfoService.getUserInfoById(id));
    }

    @PostMapping("/check_token_status")
    public RestResponse<UserInfo> checkTokenIsValid(@RequestBody TokenDTO tokenDTO) {
        return RestResponse.ok(userInfoService.parseToken(tokenDTO.getToken(), tokenDTO.getPlatform()));
    }

}
